import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

// 定义产品管理store (组合式API)
export const useProductStore = defineStore('products-getters', () => {
  // state
  const products = ref([
    { id: 1, name: '笔记本电脑', price: 5999, category: '电子产品', stock: 15 },
    { id: 2, name: '智能手机', price: 3999, category: '电子产品', stock: 20 },
    { id: 3, name: '耳机', price: 899, category: '配件', stock: 30 },
    { id: 4, name: '鼠标', price: 129, category: '配件', stock: 50 },
    { id: 5, name: '键盘', price: 329, category: '配件', stock: 40 },
    { id: 6, name: '显示器', price: 1499, category: '电子产品', stock: 10 }
  ])
  
  const nextId = ref(7)
  
  const selectedCategory = ref(null)
  
  const priceRange = ref({
    min: 0,
    max: 10000
  })
  
  // getters
  const categories = computed(() => {
    return [...new Set(products.value.map(product => product.category))]
  })
  
  const priceStats = computed(() => {
    const prices = products.value.map(product => product.price)
    return {
      min: Math.min(...prices),
      max: Math.max(...prices),
      avg: prices.reduce((sum, price) => sum + price, 0) / prices.length
    }
  })
  
  const lowStockProducts = (threshold) => {
    return products.value.filter(product => product.stock < threshold)
  }
  
  const filteredProducts = computed(() => {
    return products.value.filter(product => {
      // 分类过滤
      if (selectedCategory.value && product.category !== selectedCategory.value) {
        return false
      }
      
      // 价格范围过滤
      if (product.price < priceRange.value.min || product.price > priceRange.value.max) {
        return false
      }
      
      return true
    })
  })
  
  const productsSortedByPrice = computed(() => {
    return [...products.value].sort((a, b) => a.price - b.price)
  })
  
  const productsSortedByStock = computed(() => {
    return [...products.value].sort((a, b) => a.stock - b.stock)
  })
  
  const totalProductsCount = computed(() => {
    return products.value.length
  })
  
  const totalStock = computed(() => {
    return products.value.reduce((sum, product) => sum + product.stock, 0)
  })
  
  const totalValue = computed(() => {
    return products.value.reduce((sum, product) => sum + product.price * product.stock, 0)
  })
  
  // actions
  function addProduct(product) {
    products.value.push({
      id: nextId.value++,
      ...product
    })
  }
  
  function setCategory(category) {
    selectedCategory.value = category
  }
  
  function resetCategoryFilter() {
    selectedCategory.value = null
  }
  
  function setPriceRange(min, max) {
    priceRange.value.min = min
    priceRange.value.max = max
  }
  
  function updateStock(productId, newStock) {
    const product = products.value.find(p => p.id === productId)
    if (product) {
      product.stock = newStock
    }
  }
  
  return {
    // state
    products,
    nextId,
    selectedCategory,
    priceRange,
    // getters
    categories,
    priceStats,
    lowStockProducts,
    filteredProducts,
    productsSortedByPrice,
    productsSortedByStock,
    totalProductsCount,
    totalStock,
    totalValue,
    // actions
    addProduct,
    setCategory,
    resetCategoryFilter,
    setPriceRange,
    updateStock
  }
})
